package com.lagou.dao.impl;

import com.lagou.dao.IAccountDao;
import com.lagou.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: spring
 * @description:
 * @author: Mr.Wang
 * @create: 2020-12-15 16:06
 **/
@Repository
public class AccountDaoImpl implements IAccountDao {
	@Autowired
	private QueryRunner queryRunner;

	public List<Account> findAll() {
		String sql = "select * from account";
		List<Account> query = null;
		try {
			 query = queryRunner.query(sql, new BeanListHandler<Account>(Account.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return query;
	}

	public Account findById(Integer id) {
		String sql = "select * from id = ?";
		Account query = null;
		try {
			query = queryRunner.query(sql, new BeanHandler<Account>(Account.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return query;

	}

	public void insert(Account account) {
		String sql = "insert into account values (null,?,?)";
		try {
			queryRunner.update(sql,account.getName(),account.getMoney());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void update(Account account) {
		String sql = "update account set name = ?,money = ? where id = ?";
		try {
			queryRunner.update(sql,account.getName(),account.getMoney(),account.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void delete(Integer id) {
		String sql = "delete from account where id = ?";
		try {
			queryRunner.update(sql,id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
